package com.ebt.m.view.timeline;

import android.content.Context;
import android.text.format.Time;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.customer.net.json.CustomerDynamicJson;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.utils.CustomerActionUtil;
import com.ebt.m.utils.DateUtils;

public class InteractionViewInsuranceCreate extends IView implements View.OnClickListener, IDynamicJson {

    private TextView mTimeView;
    private TextView mContentView;
    private TextView mDateView;

    public InteractionViewInsuranceCreate(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.view_interaction_item_insurance_create, this);
        mTimeView = (TextView) findViewById(R.id.tv_time);
        mDateView = (TextView) findViewById(R.id.tv_date);
        mContentView = (TextView) findViewById(R.id.tv_content);
    }

    public InteractionViewInsuranceCreate(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InteractionViewInsuranceCreate(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... obj) {
    }

    @Override
    public void onClick(View v) {}

    @Override
    public void updateByJson(Object o) {
        if (o instanceof CustomerDynamicJson){
            CustomerDynamicJson json = (CustomerDynamicJson)o;
            try {
                Time time = new Time(Time.getCurrentTimezone());
                time.set(DateUtils.parseTimestampInSecond(json.activeTime));
                mTimeView.setText(time.hour + ":" + ((time.minute + "").length() < 2 ? "0" + time.minute : time.minute));
                if (json.showDate != null && json.showDate) {
                    mDateView.setText((time.month + 1) + "-" + time.monthDay + " " + ConfigData.WEEKSTRS[time.weekDay]);
                }
                updateActContainer(json, json.activeType);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void updateActContainer(CustomerDynamicJson json, int activeType) {
        mContentView.setMovementMethod(new LinkMovementMethod());
        CustomerActionUtil.setTextWithSpanForDyJson2(getContext(), json, activeType, mContentView);
    }

}
